package com.lz.common.utils.file;

import lombok.extern.slf4j.Slf4j;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.mp3.MP3AudioHeader;
import org.jaudiotagger.audio.mp3.MP3File;
import org.springframework.web.multipart.MultipartFile;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author caicai
 * @date 2021/8/27 17:36
 */
@Slf4j
public class AudioUtils {

    private static final Long TIME_CONVERSION = 1000L;

    /**
     * 获取音频时长 支持格式：mp3  wav
     */
    public static Integer getDuration(File file, AudioContentType contentType) {
        switch (contentType) {
            case MP3:
                return getMp3Duration(file).intValue();
            case WAV:
                return getWavDuration(file).intValue();
            default:
                log.error("不支持的音频格式,无法获取音频时长");
                return null;
        }
    }



    public static void main(String[] args) {
        //File file = new File("D:\\software\\工具\\解压工具\\网易云音乐\\resource\\c.wav");
        File file = new File("E:\\resource\\layout\\LZ-杂杂\\CSGO音乐盒\\AD8 - Sean Murray\\a.mp3");
        System.out.println(getMp3Duration(file));

//        System.out.println(getWavDuration(file));
    }


    /**
     * 获取语音文件播放时长(毫秒) 支持wav 格式
     *
     * @param wavFile
     * @return
     */
    public static Float getWavDuration(File wavFile) {
        BufferedInputStream bufferedInputStream = null;
        AudioInputStream audioInputStream = null;
        try {
            bufferedInputStream = new BufferedInputStream(new FileInputStream(wavFile));
            audioInputStream = AudioSystem.getAudioInputStream(bufferedInputStream);
            AudioFormat format = audioInputStream.getFormat();
            long audioFileLength = wavFile.length();
            int frameSize = format.getFrameSize();
            float frameRate = format.getFrameRate();
            float durationInSeconds = (audioFileLength / (frameSize * frameRate));
            return durationInSeconds * TIME_CONVERSION;
        } catch (Exception e) {
            log.error("音频时长解析错误", e);
            return 0f;
        } finally {
            try {
                if (audioInputStream != null) {
                    audioInputStream.close();
                }
                if (bufferedInputStream != null) {
                    bufferedInputStream.close();
                }
            } catch (IOException e) {
                log.error("字节流关闭错误", e);
            }
        }

    }

    /**
     * 获取mp3语音文件播放时长(毫秒)
     *
     * @param mp3File
     * @return
     */
    public static Float getMp3Duration(File mp3File) {

        try {
            MP3File f = (MP3File) AudioFileIO.read(mp3File);
            MP3AudioHeader audioHeader = (MP3AudioHeader) f.getAudioHeader();
            return Float.parseFloat(audioHeader.getPreciseTrackLength() + "") * TIME_CONVERSION;
        } catch (Exception e) {
            log.error("音频时长解析错误", e);
            return 0f;
        }
    }

    /**
     * 获取音频名称
     */
    public static String getAudioName(MultipartFile audio) {
        return audio.getOriginalFilename().split("\\.")[0];
    }
}